#include "stdafx.h"

#include "EngineTests.h"

// Logs bitboard 
void LogBitBoard(int logId, BitBoard bb)
{
	bool bPrintBefore = true;
	for(int i = 31; i >= 0; i-=4)
	{
		for(int j = i - 3; j <= i; j++)
		{
			if(bPrintBefore)
				LogMessage(logId, ".");

			// choose figure
			int wm = bb.wm & (1 << j);
			int wk = bb.wk & (1 << j);
			int bm = bb.bm & (1 << j);
			int bk = bb.bk & (1 << j);

			if(wm)
				LogMessage(logId, "w");
			else if(wk)
				LogMessage(logId, "W");
			else if(bm)
				LogMessage(logId, "b");
			else if(bk)
				LogMessage(logId, "B");
			else
				LogMessage(logId, "0");

			if(! bPrintBefore)
				LogMessage(logId, ".");
		}

		LogMessage(logId, "\n");
		bPrintBefore =  !bPrintBefore;
	}

	LogMessage(logId, "\n");
}


// logs the move
void LogMove(int logId, BitBoard bb)
{
	uint32 result = bb.wm | bb.wk | bb.bm | bb.bk;

	bool oFirst = true;
	
	for(int i = 0; i < 32; i++)
	{
		int bit = result & ( 1 << i );
		if(bit)
		{
			LOG_MSG(logId, "%d", i);
			
			if(oFirst)
				oFirst = false;
			else
				LogMessage(logId, " - ");
		}
	} // for

	LogMessage(logId, "\n");
}


BYTE ConvertFigureType(char figure)
{
	if(figure == 'w')
		return WHITE_MAN;
	else if(figure == 'b')
		return BLACK_MAN;
	else if(figure == 'W')
		return WHITE_King;
	else if(figure == 'B')
		return BLACK_King;
	else
		return EMPTY;
}


void SetupBoard(TGameBoard computerBoard, char board[8][8])
{
	BYTE rowIndex[8] = { 5, 10, 14, 19, 23, 28, 32, 37 };

	ClearBoard(computerBoard);

	for(int row = 0; row < 8; row++)
		for(int col = 0; col < 8; col++)
		{
			if(board[row][col] != 0)
			{
				BYTE colIndex = row % 2 == 0 ? col / 2 : (col - 1) / 2;
				BYTE index = rowIndex[ row ]  + colIndex;
				computerBoard[ index ] = ConvertFigureType( board[row][col] );
			}
		} // for
}


char cStartUpPosition[8][8] = 
{
	{'w', 0 , 'w', 0, 'w', 0,'w', 0},
	{ 0, 'w', 0, 'w', 0, 'w', 0, 'w'},
	{'w', 0 , 'w', 0, 'w', 0,'w', 0},
	{0,   0 ,  0,  0,  0,  0, 0,  0},
	{0,   0 ,  0,  0,  0,  0, 0,  0},
	{ 0, 'b', 0, 'b', 0, 'b', 0, 'b'},
	{'b', 0 , 'b', 0, 'b', 0,'b', 0},
	{ 0, 'b', 0, 'b', 0, 'b', 0, 'b'}
};


void SetStartUpPositionGameBoard(TGameBoard board)
{
	SetupBoard(board, cStartUpPosition);
}

void SetStartUpPositionBitBoard(BitBoard* bb)
{
	TGameBoard tempBoard;
	SetStartUpPositionGameBoard(tempBoard);
	ConvertBoardToBitboard(tempBoard, bb);
}
